package cn.lili.modules.file.plugin;

import cn.hutool.json.JSONUtil;
import cn.lili.common.exception.ServiceException;
import cn.lili.modules.file.entity.enums.OssEnum;
import cn.lili.modules.file.plugin.impl.AliFilePlugin;
import cn.lili.modules.file.plugin.impl.HuaweiFilePlugin;
import cn.lili.modules.file.plugin.impl.MinioFilePlugin;
import cn.lili.modules.file.plugin.impl.TencentFilePlugin;
import cn.lili.modules.system.client.SettingClient;
import cn.lili.modules.system.entity.dos.Setting;
import cn.lili.modules.system.entity.dto.OssSetting;
import cn.lili.modules.system.entity.enums.SettingEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 文件服务抽象工厂 直接返回操作类
 *
 * @author Chopper
 * @version v1.0
 * 2022-06-06 11:35
 */
@Component
@RequiredArgsConstructor
public class FilePluginFactory {


    private final SettingClient settingClient;


    /**
     * 获取oss client
     *
     * @return
     */
    public FilePlugin filePlugin() {

        OssSetting ossSetting;
        try {
            Setting setting = settingClient.get(SettingEnum.OSS_SETTING.name());

            ossSetting = JSONUtil.toBean(setting.getSettingValue(), OssSetting.class);


            return switch (OssEnum.valueOf(ossSetting.getType())) {
                case MINIO -> new MinioFilePlugin(ossSetting);
                case ALI_OSS -> new AliFilePlugin(ossSetting);
                case HUAWEI_OBS -> new HuaweiFilePlugin(ossSetting);
                case TENCENT_COS -> new TencentFilePlugin(ossSetting);
            };
        } catch (Exception e) {
            throw new ServiceException();
        }
    }


}
